/*
 * @Author: hej 2115735990@qq.com
 * @Date: 2024-03-15 13:44:53
 * @LastEditors: hej 2115735990@qq.com
 * @LastEditTime: 2024-03-20 16:32:36
 * @FilePath: \Vue3-big-event-admin-demo\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import { ElMessage } from 'element-plus'
// import HomeView from '../views/HomeView.vue'
// createRouter用来创建实例

// 配置history模式
// history模式：createWebHistory  地址栏不带#号
// hash模式：createHashHistory   地址栏带#号
// Vite中的环境变量import.meta.env.BASE_URL
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue')
    },
    // 首页架子
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/article/manage',
      children: [
        // 文章管理
        {
          path: '/article/manage',
          component: () => import('@/views/article/ArticleManage.vue')
        },
        {
          path: '/article/channel',
          component: () => import('@/views/article/ArticleChannel.vue')
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        },
        {
          path: '/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        }
      ]
    }
  ]
})
// 登录访问拦截--默认直接放行
// 根据返回值确定放行还是拦截(undefine/true---放行；false拦截)
router.beforeEach((to) => {
  // to.path获取当前访问路径
  // 查询是否有toekn存在，并且是否访问登录页，如果不是则拦截，强制跳到登录页,其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') {
    ElMessage.error('还没有登录')
    return '/login'
  }
})
export default router
